package com.company.cloud.mall.modules.mgr.controller;

import java.io.Serializable;

import com.company.cloud.common.auth.annotation.StaffLogc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.cloud.common.auth.annotation.Authc;
import com.company.cloud.common.bean.BasePageParam;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.bean.LoginUserInfo;
import com.company.cloud.common.log.annotation.SysLog;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.mall.modules.base.entity.CartInfo;
import com.company.cloud.mall.modules.base.service.CartInfoService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 购物车管理
 */
@Authc
@RestController
@RequestMapping("/mgr/mall/cart")
@Api(tags="Shop_Mgr_购物车管理")
public class CartMgrController {

	@Autowired
	private CartInfoService cartInfoService;
	
	@PostMapping("/query")
	@ApiOperation(value="购物车信息表列表查询", response=BaseResult.class)
	public BaseResult query(@RequestBody BasePageParam params) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		IPage<CartInfo> data = cartInfoService.page(
				new Page<>(params.getPage(), params.getRows()), 
				new QueryWrapper<CartInfo>()
				.eq("company_no", userInfo.getCompanyNo())
				);
		return BaseResult.ok(data);
	}
	
	@GetMapping("/get")
	@SysLog("购物车信息表详情")
	@ApiOperation(value="购物车信息表详情")
	public BaseResult get(@RequestParam("id")Serializable id) {
		CartInfo result = cartInfoService.getById(id);
		return BaseResult.ok(result);
	}
	
	@PostMapping("/save")
	@SysLog("购物车信息表新增")
	@ApiOperation(value="购物车信息表新增")
	@StaffLogc(title = "新增购物车", action = "新增购物车", actionAdo = "购物车")
	public BaseResult save(@RequestBody CartInfo params) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		params.addCreateParam(userInfo);
		params.addEditParam(userInfo);	
		cartInfoService.save(params);
		
		return BaseResult.ok();
	}
	
	@PostMapping("/update")
	@SysLog("购物车信息表修改")
	@ApiOperation(value="购物车信息表修改")
	@StaffLogc(title = "修改购物车", action = "修改购物车", actionAdo = "购物车")
	public BaseResult update(@RequestBody CartInfo params) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		params.addEditParam(userInfo);
		cartInfoService.updateById(params);
		
		return BaseResult.ok();
	}
	
	@GetMapping("/delete")
	@SysLog("购物车信息表删除")
	@ApiOperation(value="购物车信息表删除")
	@StaffLogc(title = "删除购物车", action = "删除购物车", actionAdo = "购物车")
	public BaseResult delete(@RequestParam("id")Serializable id) {
		cartInfoService.removeById(id);
		return BaseResult.ok();
	}
	
}
